﻿/**
* DragRegion 区域内查看图片
* @author lbynet
* @version 0.1 2008.3.26
* 
* new DragRegion(mc,maskMc)
*/
package com._public._control 
{
	import com._public._transitions.transitions.Tweener;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class DragRegion
	{
		
		private var content_mc:DisplayObject;
		private var mask_mc:DisplayObject;
		
		public function DragRegion(_contentMC:DisplayObject, _maskMC:DisplayObject) 
		{
			
			this.content_mc = _contentMC;
			this.mask_mc = _maskMC;
			
			content_mc.mask = mask_mc;
			
			addListeners();
		}
		
		public function addListeners():void 
		{
			content_mc.addEventListener(MouseEvent.ROLL_OVER, eventHander);
			content_mc.addEventListener(MouseEvent.ROLL_OUT, eventHander);
		}
		
		public function removeListeners():void 
		{
			content_mc.removeEventListener(MouseEvent.ROLL_OVER, eventHander);
			content_mc.removeEventListener(MouseEvent.ROLL_OUT, eventHander);
		}
		private function eventHander(event:MouseEvent):void 
		{
			
			switch (event.type) 
			{
				case MouseEvent.ROLL_OVER :
					
					content_mc.addEventListener(MouseEvent.MOUSE_MOVE, moveHander);
					break;
				case MouseEvent.ROLL_OUT :
					
					content_mc.removeEventListener(MouseEvent.MOUSE_MOVE, moveHander);
					break;
			}
		}
		
		private function moveHander(event:MouseEvent):void 
		{
			var _width:Number = mask_mc.width;
			var _height:Number = mask_mc.height;
			var _newX:Number = content_mc.x;
			var _newY:Number = content_mc.y;
			if (_width < content_mc.width)
			{
				_newX = -((content_mc.width - _width) / _width) * mask_mc.mouseX + mask_mc.x;
				Tweener.addTween(content_mc, { x:_newX, time:0.5 } );
			}
			if (_height < content_mc.height)
			{
				_newY = -((content_mc.height - _height) / _height) * mask_mc.mouseY + mask_mc.y;
				Tweener.addTween(content_mc, { y:_newY, time:0.5 } );
			}
		}
		
	}
}